﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.22
 * 描 述：银行账号
 */
var selectedRow;
var refreshGirdData;
var userdefault = {};
var bootstrap = function ($, learun) {
    "use strict";
    var companyId = '';
    var departmentId = '';
    var category = '';

    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);
            page.bind();
            page.initGrid();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 公司选择
            $('#company_select').lrselect({
                type: 'tree',
                // placeholder: '请选择机构',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                //allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        companyId = item.value;
                        var keyword = $('#txt_Keyword').val();

                        $('#department_select').lrselectRefresh({
                            // 访问数据接口地址
                            url: config.base_server() + 'departments/tree?access_token=' + config.getToken().access_token,
                            // 访问数据接口参数
                            param: {companyId: companyId, parentId: '0'},
                        });
                        page.search({keyword: keyword});
                    }
                }
            });
            // 部门选择
            $('#department_select').lrselect({
                type: 'tree',
                placeholder: '请选择部门',
                // 是否允许搜索
                allowSearch: true,
                select: function (item) {
                    if (!item || item.value == '-1') {
                        departmentId = '';
                    } else {
                        departmentId = item.value;
                    }
                    page.search();
                }
            });

            // 员工类型选择
            $('#category_select').lrDataItemSelect({
                code: 'UserCategorys',
                placeholder: "请选择员工类型",
                select: function (item) {
                    if (!item || item.id == '-1') {
                        category = '';
                    } else {
                        category = item.id;
                    }
                    page.search();
                }
            });

            $('#company_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });

            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });

            // 从excel导入
            $('#lr_import').on('click', function () {
                learun.layerForm({
                    id: 'UploadifyForm',
                    title: '上传文件',
                    url: config.webportal_server + 'pages/salary/bankAccount/uploadifyForm.html?access_token=' + config.getToken().access_token,
                    width: 600,
                    height: 400,
                    callBack: function (id) {
                        return top[id].acceptClick(function (datas) {
                            if (datas) {
                                refreshGirdData();
                            }
                        });
                    }
                });
            });

            //保存
            $('#lr_save').on('click', function () {
                var productData = [];
                var productDataTmp = $('#gridtable').jfGridGet('rowdatas');

                if (productDataTmp.length == 0) {
                    learun.alert.error('表单信息不全,请检查！');
                    return false;
                }

                for (var i = 0, l = productDataTmp.length; i < l; i++) {
                    productData.push(productDataTmp[i]);
                }

                var postData = productData;
                learun.layerConfirm('注：您确认要保存吗？', function (res, index) {
                    if (res) {
                        $.lrSaveForm(config.base_server() + 'users/saveBankAccount?access_token=' + config.getToken().access_token, JSON.stringify(postData), function (res) {
                            if (res.resp_code == 200) {
                                refreshGirdData();
                            }
                        });
                    }
                    top.layer.close(index); //再执行关闭
                });
            });

        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'users/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: '账户', name: 'userName', width: 100, align: 'left'},
                    {label: '姓名', name: 'realName', width: 160, align: 'left'},
                    {
                        label: '性别', name: 'sex', width: 45, align: 'center',
                        formatter: function (cellvalue) {
                            if (cellvalue == "2") {
                                return "女";
                            } else if (cellvalue == "1") {
                                return "男";
                            } else {
                                return "";
                            }
                        }
                    },
                    {
                        label: '职级', name: 'levels', width: 80, align: 'center'
                    },
                    {label: '手机', name: 'phone', width: 100, align: 'center'},
                    {
                        label: '部门', name: 'departmentId', width: 100, align: 'center',
                        formatterAsync: function (callback, value, row) {
                            learun.clientdata.getAsync('department', {
                                key: value,
                                callback: function (item) {
                                    callback(item.name);
                                }
                            });
                        }
                    },
                    {
                        label: "状态", name: "status", index: "status", width: 50, align: "center",
                        formatter: function (cellvalue) {
                            if (cellvalue == 1) {
                                return '<span class=\"label label-success\" style=\"cursor: pointer;\">正常</span>';
                            } else if (cellvalue == 0) {
                                return '<span class=\"label label-default\" style=\"cursor: pointer;\">禁用</span>';
                            }
                        }
                    },
                    {
                        label: "银行账号", name: "bankAccount", width: 200, align: "left",
                        edit: {
                            type: 'input'
                        }
                    },
                ],
                rows: 1500,
                isPage: false,
                reloadSelected: true,
                isTree: false,
                isEdit: true,
                mainId: 'id'
            });
        },
        search: function (param) {
            param = param || {};
            param.companyId = companyId;
            param.departmentId = departmentId;
            param.category = category;

            $('#gridtable').jfGridSet('reload', param);
        }
    };

    refreshGirdData = function () {
        var keyword = ('#txt_Keyword').val();
        page.search({keyword: keyword});
    };

    page.init();
}


